package com.sitia.jd.service.impl;

import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.kplunion.promotioncommon.PromotionService.request.get.PromotionCodeReq;
import com.jd.open.api.sdk.domain.kplunion.promotioncommon.PromotionService.response.get.GetResult;
import com.jd.open.api.sdk.request.kplunion.UnionOpenPromotionCommonGetRequest;
import com.jd.open.api.sdk.response.kplunion.UnionOpenPromotionCommonGetResponse;
import com.sitia.common.core.domain.AjaxResult;
import com.sitia.common.utils.StringUtils;
import com.sitia.jd.domain.MxOrder;
import com.sitia.jd.domain.dto.JdRebateParamsInfo;
import com.sitia.jd.service.IJdService;
import com.sitia.system.service.ISysConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 对京东操作的实现类型
 */
@Service
public class JdServiceImpl implements IJdService {
    @Resource
    private ISysConfigService sysConfigService;

    /**
     * 获取京东登陆二维码
     * @return
     */
    @Override
    public String getJdQrCode() {

        return null;
    }

    @Override
    public AjaxResult getRebateUrl(MxOrder mxOrder) {
        String appKey = sysConfigService.getValueByKey("jd_union_app_key", "22233", "京东联盟appKey");
        String appSecret = sysConfigService.getValueByKey("jd_union_app_secret", "22233", "京东联盟app_secret");
        String SERVER_URL = sysConfigService.getValueByKey("jd_union_server_url", "https://api.jd.com/routerjson", "京东联盟服务地址");
        UnionOpenPromotionCommonGetRequest request=new UnionOpenPromotionCommonGetRequest();
        JdClient client=new DefaultJdClient(SERVER_URL,null,appKey,appSecret);
        PromotionCodeReq promotionCodeReq=new PromotionCodeReq();
        //设置请求参数
        String siteId = sysConfigService.getValueByKey("jd_union_site_id", "4101541880", "京东联盟流媒体id");
        promotionCodeReq.setSiteId(siteId);
        // 设置商品链接
        promotionCodeReq.setMaterialId(mxOrder.getGoodsUrl());
        promotionCodeReq.setSceneId(1);
        request.setPromotionCodeReq(promotionCodeReq);
        request.setVersion("1.0");
        try {
            UnionOpenPromotionCommonGetResponse response=client.execute(request);
            GetResult getResult = response.getGetResult();
            return AjaxResult.success(getResult.getData());
        } catch (Exception e) {
            return AjaxResult.error("获取失败");
        }

    }

    @Override
    public GetResult getRebateInfo(JdRebateParamsInfo jdRebateParamsInfo) {
        UnionOpenPromotionCommonGetRequest request=new UnionOpenPromotionCommonGetRequest();
        String serverUrl=jdRebateParamsInfo.getServerUrl();
        if (StringUtils.isEmpty(jdRebateParamsInfo.getServerUrl())) {
            serverUrl = sysConfigService.getValueByKey("jd_union_server_url", "https://api.jd.com/routerjson", "京东联盟服务地址");
        }
        JdClient client=new DefaultJdClient(serverUrl,null,jdRebateParamsInfo.getAppKey(),jdRebateParamsInfo.getAppSecret());
        PromotionCodeReq promotionCodeReq=new PromotionCodeReq();
        //设置请求参数
        promotionCodeReq.setSiteId(jdRebateParamsInfo.getSiteId());
        // 设置商品链接
        promotionCodeReq.setMaterialId(jdRebateParamsInfo.getMaterialId());
        promotionCodeReq.setSceneId(jdRebateParamsInfo.getSceneId());
        request.setPromotionCodeReq(promotionCodeReq);
        request.setVersion("1.0");
        try {
            UnionOpenPromotionCommonGetResponse response=client.execute(request);
            return response.getGetResult();
        } catch (Exception e) {
            GetResult getResult = new GetResult();
            getResult.setMessage("京东联盟接口获取异常");
            getResult.setCode(500);
            return getResult;
        }
    }
}
